package com.example.vv.model.project.base.presenter;

import com.example.vv.fragmentWork.entity.TreeClassEntity;
import com.example.vv.fragmentWork.mvp.MvpPresenter;
import com.example.vv.model.project.base.contract.ChatContract;
import com.example.vv.model.project.base.model.ChatModel;
import com.example.vv.res.net.BaseResponse;
import com.example.vv.res.net.NetUrl;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public class ChatPresenter extends MvpPresenter<ChatContract.View, ChatModel> implements ChatContract.Presenter{

    @Override
    public void attach(ChatContract.View view) {
        super.attach(view);
        model = new ChatModel();
    }


    @Override
    public void setClassData() {
        model.GET_CLASS(NetUrl.GET_CLASS_TREE)
                .map(new Function<BaseResponse, List<TreeClassEntity>>() {
                    @Override
                    public List<TreeClassEntity> apply(BaseResponse baseResponse) throws Exception {

                        return new Gson().fromJson(baseResponse.getData(),new TypeToken<List<TreeClassEntity>>(){}.getType());
                    }
                })
                .subscribe(new Consumer<List<TreeClassEntity>>() {
                    @Override
                    public void accept(List<TreeClassEntity> classEntities) throws Exception {
                        view().getClassData(classEntities);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                });
    }
}
